package com.william.killian.trivia;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ToggleButton;

import com.william.killian.trivia.model.Answer;
import com.william.killian.trivia.util.Util;

public class QuestionEditor extends ListActivity
{
    private int index;
    private AnswerAdapter mAdapter;
    private ListView mList;
    
    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setTheme (android.R.style.Theme_Light);
        
        index = getIntent ().getIntExtra ("index", 0) - 1;
        mList = getListView ();
        mAdapter = new AnswerAdapter ();
        
        Button add = new Button (this);
        add.setText (R.string.text_answer_add);
        add.setOnClickListener (new View.OnClickListener()
        {
            public void onClick (View v)
            {
                Util.currentQuiz ().getQuestions ().get (index).getAnswers ().add (new Answer ("New Answer"));
                mAdapter.notifyDataSetChanged ();
            }
        });
        
        final EditText title = new EditText (this);
        title.setText (Util.currentQuiz ().getQuestions ().get (index).getText ());
        title.setTextAppearance (this, R.style.question_title_style);
        title.setOnFocusChangeListener (new OnFocusChangeListener()
        {
            public void onFocusChange (View v, boolean hasFocus)
            {
                Util.currentQuiz ().getQuestions ().get (index).setText (title.getText ().toString ());
            }
        });
        

        Button save = new Button (this);
        save.setText (R.string.save);
        save.setOnClickListener (new View.OnClickListener()
        {
            public void onClick (View v)
            {
                Util.currentQuiz ().save ();
                finish ();
            }
        });
        
        mList.addHeaderView (title);
        
        LinearLayout ll = new LinearLayout (this);
        ll.setOrientation (LinearLayout.HORIZONTAL);
        ll.addView (save);
        ll.addView (add);
        
        mList.addFooterView (ll);
        
        setListAdapter (mAdapter);
    }
    
    private class AnswerAdapter extends BaseAdapter
    {
        
        public AnswerAdapter () {
            
        }

        public View getView (final int pos, View convertView, ViewGroup parent)
        {
            //pos --; // has parent
            final int position = pos;
            if (convertView == null)
            {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.answer_item, null);
            }
            final EditText name = (EditText) convertView.findViewById(R.id.edit);
            final ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.button);
            
            name.setText (Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).getText ());
            toggle.setChecked (Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).isCorrect ());
            
            toggle.setOnCheckedChangeListener (new OnCheckedChangeListener()
            {
                public void onCheckedChanged (CompoundButton b, boolean checked)
                {
                    Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).setCorrect (checked);
                }
            });
            
            name.setOnFocusChangeListener (new OnFocusChangeListener()
            {
                public void onFocusChange (View arg0, boolean arg1)
                {
                    Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).setName (name.getText ().toString ());
                }
            });
            
            convertView.setOnFocusChangeListener (new OnFocusChangeListener()
            {
                public void onFocusChange (View v, boolean hasFocus)
                {
                    Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).setName (name.getText ().toString ());
                    Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (position).setCorrect (toggle.isChecked ());
                }
            });
            
            return convertView;
        }

        public int getCount ()
        {
            return Util.currentQuiz ().getQuestions ().get (index).getAnswers ().size ();
        }

        public Object getItem (int arg0)
        {
            return Util.currentQuiz ().getQuestions ().get (index).getAnswers ().get (arg0 - 1);
        }

        public long getItemId (int arg0)
        {
            return (arg0);
        }
        
    }
    
    public void onPause ()
    {
        super.onPause ();
        Util.currentQuiz ().save ();
        finish ();
    }
}
